package com.thon.controller.rest;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.entity.project.ProjectBudget;
import com.thon.entity.project.ProjectSchedule;
import com.thon.service.project.ProjectBudgetService;

/**
 * @file ProjectBudgetRestController.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月12日 下午6:20:08
 * @description: 有关请求项目预算数据的接口
 */
@Controller
@RequestMapping(value = "/api/project/budget")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ProjectBudgetRestController {

	@Autowired
	private ProjectBudgetService projectBudgetService;

	// 根据项目id更新项目预算
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> updateProjectBudget(@Valid @RequestBody ProjectBudget projectBudget) {

		projectBudgetService.updateBudget(projectBudget);
		return new ResponseEntity(projectBudget, HttpStatus.CREATED);
	}

	// 根据项目预算id获取项目预算
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> getBudget(@RequestParam("id") int id, Model model) {

		ProjectBudget projectBudget = projectBudgetService.getBudget(id);
		return new ResponseEntity(projectBudget, HttpStatus.OK);
	}

	// 根据project Id获取该项目的预算列表
	@RequestMapping(value = "/list")
	public ResponseEntity<HttpEntity> listProjectBudget(@RequestParam(value = "pid", required = true) int projectId,
			Model model) {

		List<ProjectBudget> projectBudgets = projectBudgetService.listProjectBudgets(projectId);
		model.addAttribute("budget", projectBudgets);
		return new ResponseEntity(model, HttpStatus.OK);
	}

	// 添加预算反馈接口
	@RequestMapping(value = "/feedback/add", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> addFeedback(@Valid @RequestBody ProjectBudget projectBudget, Model model) {

		ProjectBudget oldProjectBudget = new ProjectBudget();
		oldProjectBudget = projectBudgetService.getProjectBudget(projectBudget.getStage(), projectBudget
				.getProject().getId());

		oldProjectBudget.setFeedback(projectBudget.getFeedback());
		projectBudgetService.updateBudget(oldProjectBudget);

		model.addAttribute("projectBudget", oldProjectBudget);
		return new ResponseEntity(model, HttpStatus.CREATED);
	}

}
